package com.han.hotelplat.service.hotel.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.han.hotelplat.comm.Constant;
import com.han.hotelplat.comm.base.EntityManage;
import com.han.hotelplat.comm.base.Pager;
import com.han.hotelplat.comm.utils.AreaHelp;
import com.han.hotelplat.comm.utils.DateUtil;
import com.han.hotelplat.dao.hotel.HotelDao;
import com.han.hotelplat.dao.wallet.WalletDao;
import com.han.hotelplat.pojo.hotel.Hotel;
import com.han.hotelplat.pojo.hotelroom.HotelRoom;
import com.han.hotelplat.pojo.wallet.Wallet;
import com.han.hotelplat.service.hotel.HotelService;
import com.han.hotelplat.service.hotelroom.HotelRoomService;
import com.han.hotelplat.service.sysuserhotel.SysUserHotelService;

@Service
public class HotelServiceImpl implements HotelService{
	@Autowired
	private HotelDao hotelDao;
	@Autowired
	private WalletDao walletDao;
	@Autowired
	private HotelRoomService hotelRoomService;
	@Autowired
	private EntityManage entityManage;
	@Autowired
	private SysUserHotelService sysUserHotelService;
	
	@Override
	public List<Hotel> getAllHotel() {
		return hotelDao.findAll();
	}

	@Override
	public Map<String, Object> saveHotel(Hotel hotel, String userid) {
		Map<String , Object> returndata = new HashMap<String, Object>();
		hotel.setState(Constant.HOTEL_STATE_WAITAUTH);//数据被修改则需要将状态置为待审核
		if(hotel.getId() != null){
			hotelDao.update(hotel);
			returndata.put("code", 0);
			returndata.put("message", "数据保存成功");
			return returndata;
		}
		hotelDao.save(hotel);
		List<String> bindAlHotelUser = sysUserHotelService.getAllUserBindAllHotel();
		bindAlHotelUser.add(userid);
		//将酒店绑定到用户身上
		for(String userId : bindAlHotelUser){
			sysUserHotelService.bindingHotel(userId, hotel.getId());
		}
		//创建酒店钱包
		Wallet wallet = new Wallet();
		wallet.setUserid(hotel.getId().toString());
		wallet.setUsertype(Constant.WALLET_USER_TYPE_HOTEL);
		wallet.setState(Constant.WALLET_STATE_NOTACTIVE);
		wallet.setBalance(0f);
		wallet.setOverdraft(0f);
		walletDao.save(wallet);
		//TODO 记录修改日志
		returndata.put("code", 0);
		returndata.put("message", "数据保存成功");
		return returndata;
	}

	@Override
	public Hotel getHotelById(Integer hotelid) {
		List<HotelRoom> rooms = hotelRoomService.findHotelRoomByHotelId(hotelid);
		Hotel h = hotelDao.findById(hotelid);
		if(h != null){
			h.setRooms(rooms);
		}
		return h;
	}

	@Override
	public void update(Hotel hotel) {
		hotelDao.update(hotel);
	}

	@Override
	public Pager<Hotel> findHoteByPage(String startDate, String endDate,
			Integer state, Integer sortType, Integer areaCode,
			String condition, Integer pageNo, Integer isArrears) {
		StringBuffer sbf = new StringBuffer( " from  tb_wallet w ,tb_hotel h "
				+ " LEFT JOIN tb_hotel_hmanager AS hhm ON hhm.hotelid=h.id"
				+ " where w.userid=h.id " );
		if(startDate != null && !startDate.isEmpty()){
			sbf.append(" and h.creattime >= '"+startDate+" 00:00:00'");
		}
		if(endDate != null && !endDate.isEmpty()){
			sbf.append(" and h.creattime <= '"+endDate+" 23:59:59'");
		}
		if(state != null  && state.intValue() != 0){
			sbf.append(" and h.state ="+state);
		}else{
			sbf.append(" and h.state <>"+Constant.HOTEL_STATE_STOP);
		}
		if(condition != null && !condition.isEmpty()){
			sbf.append(" and (h.hotelname like '%"+condition+"%'"
					+ " or hhm.hmtel like '%"+condition+"%'"
					+ " or hhm.hmname like '%"+condition+"%')");
		}
		
		if(areaCode != null && areaCode.intValue()>100000){//区域码至少6位
			String w_areaCode = areaCode.toString();
			int x=0;
			String w_area="";
			if(w_areaCode.endsWith("0000")) {
				//精度 省份
				x=9999;
			}else if(w_areaCode.endsWith("00")) {
				//精度 市
				x=99;
			}else {
				//精度 区
				x=0;
			}
			//要的   - 条件  >=0
			//410610-410000 <= 9999
			//410610-410600 <= 99
			//410610-410610 == 0
			w_area=" and convert(h.area,signed) - "+areaCode +" >=0 ";
			w_area+=" and convert(h.area,signed) - "+areaCode +" <= "+x;
			sbf.append(w_area);
		}
		
		if(isArrears != null && isArrears.intValue() == 1){//有欠款
			sbf.append(" and w.balance<0 ");
		}
		
		if(isArrears != null && isArrears.intValue() == 2){//无欠款
			sbf.append(" and w.balance>=0 ");
		}
		
		// 放到语句最后面
		if(sortType != null && sortType.intValue() == 1){//按创建时间降序
			sbf.append(" order by h.creattime desc");
		}else if(sortType != null && sortType.intValue() == 2){//按创建时间升序
			sbf.append(" order by h.creattime asc");
		}else if(sortType != null && sortType.intValue() == 3){//按钱包余额降序
			sbf.append(" order by w.balance desc");
		}else if(sortType != null && sortType.intValue() == 4){//按钱包余额升序
			sbf.append(" order by w.balance asc");
		}else{
			sbf.append(" order by h.creattime desc");
		}
		
		String fromSql = sbf.toString();
		sbf.insert(0, " select DISTINCT(h.id) as id, h.hotelname as hotelname,h.area as area, h.hoteltype as hoteltype, "
				+ " h.creattime as creattime, h.state as state, w.balance as balance, w.overdraft as overdraft, "
				+ " (SELECT COUNT(subhhm.hmuserid) "
				+ " FROM tb_hotel_hmanager AS subhhm "
				+ " WHERE h.id=subhhm.hotelid and subhhm.state = 1) AS count ");
		
		short pageSize = 20;
		Pager data = entityManage.findBySqlPage(sbf.toString(),fromSql, pageNo, pageSize);
		List<Hotel> hli = new ArrayList<Hotel>();
		List li = data.getList();
		for(int i= 0 ; i<li.size(); i++){
			Object[] ory = (Object[]) li.get(i);
			Hotel h = new Hotel();
			if(ory[0] != null){
				h.setId(Integer.parseInt(ory[0].toString()));
			}
			if(ory[1] != null){
				h.setHotelname(ory[1].toString());
			}
			if(ory[2] != null){
				if(ory[2] != null){
					h.setArea(Integer.parseInt(ory[2].toString()));
					h.setAreaModel(AreaHelp.getInstance().getAreaModels(h.getArea().toString()));
				}
			}
			if(ory[3] != null){
				h.setHoteltype(Integer.parseInt(ory[3].toString()));
			}
			
			if(ory[4] != null){
				h.setCreattime(DateUtil.parseYMDHMS(ory[4].toString()));
			}
			if(ory[5] != null){
				h.setState(Integer.parseInt(ory[5].toString()));
			}
			if(ory[6] != null){
				h.setBalance(Float.valueOf(ory[6].toString()));
			}
			if(ory[7] != null){
				h.setOverdraft(Float.valueOf(ory[7].toString()));
			}
			if(ory[8] != null){
				h.setUsercount(Integer.valueOf(ory[8].toString()));
			}
			
			hli.add(h);
		}
		data.setList(hli);
		return data;
	}
	
	@Override
	public List<Hotel> getHotelByConditione(String hotelName, String userid) {
		String sql = "select h.* from tb_sysuser_hotel as sh, tb_hotel as h left join tb_hotel_hmanager as hhm "
				+ "on hhm.hotelid = h.id where sh.hotelid = h.id ";
		if(userid != null){
			sql = sql+ "and sh.userid='"+userid +"'";
		}
		sql = sql+" and (h.hotelname like '%"+hotelName+"%' or hhm.hotelname like '%"+hotelName+"%'"
				+ " or hhm.hmtel like '%"+hotelName+"%'"+" or hhm.hmname like '%"+hotelName+"%'"
				+ ") group by h.id";
		List<Hotel> data = hotelDao.findBySql(sql);
		
		return data;
	}

	@Override
	public List<Hotel> exportHotelList(String startDate, String endDate,
			Integer state, Integer sortType, Integer areaCode,
			String condition, Integer isArrears) {
		StringBuffer sbf = new StringBuffer( " from Hotel as h,  Wallet as w where w.userid = h.id");
		if(startDate != null && !startDate.isEmpty()){
			sbf.append(" and h.creattime >= '"+startDate+" 00:00:00'");
		}
		if(endDate != null && !endDate.isEmpty()){
			sbf.append(" and h.creattime <= '"+endDate+" 23:59:59'");
		}
		if(state != null  && state.intValue() != 0){
			sbf.append(" and h.state ="+state);
		}
		if(condition != null && !condition.isEmpty()){
			sbf.append(" and (h.hotelname like '%"+condition+"%')");
		}
		
		if(areaCode != null && areaCode.intValue()>100000){//区域码至少6位
			String w_areaCode = areaCode.toString();
			int x=0;
			String w_area="";
			if(w_areaCode.endsWith("0000")) {
				//精度 省份
				x=9999;
			}else if(w_areaCode.endsWith("00")) {
				//精度 市
				x=99;
			}else {
				//精度 区
				x=0;
			}
			//要的   - 条件  >=0
			//410610-410000 <= 9999
			//410610-410600 <= 99
			//410610-410610 == 0
			w_area=" and convert(h.area,signed) - "+areaCode +" >=0 ";
			w_area+=" and convert(h.area,signed) - "+areaCode +" <= "+x;
			sbf.append(w_area);
		}
		
		if(isArrears != null && isArrears.intValue() == 1){//有欠款
			sbf.append(" and w.balance<0 ");
		}
		
		if(isArrears != null && isArrears.intValue() == 2){//无欠款
			sbf.append(" and w.balance>=0 ");
		}
		
		// 放到语句最后面
		if(sortType != null && sortType.intValue() == 1){//降序
			sbf.append(" order by h.creattime desc");
		}else if(sortType != null && sortType.intValue() == 2){
			sbf.append(" order by h.creattime asc");
		}else{
			sbf.append(" order by h.creattime desc");
		}
		sbf.insert(0, "select new Hotel(h.id, h.hotelname, h.area, h.hoteltype,"
				+ " h.creattime,h.state,w.balance, w.overdraft,h.level, h.emunitprice,h.unitprice) ");
		return hotelDao.findByHQL(sbf.toString());
	}
	
}
